"use strict"
/**
 * @class elFinder command "customEdit". 
 * Edita las propiedades de cada archivo en particular
 * 
 * @todo Hacerlo bien!!
 *
 * @author diego olmedo
 **/
elFinder.prototype.commands.customEdit = function() {
    var self  = this,
    fm    = this.fm,
		
    /**
    * Open dialog with textarea to edit file
    *
    * @param  String  id       dialog id
    * @param  Object  file     file object
    * @param  String  content  file content
    * @return $.Deferred
    **/
    dialog = function(id, file, content) {
        var editTemplate = '';
        $.ajax({
            type: "GET",   
            url: "editTemplates/imageEditTemplate.php",   
            async: false,
            success : function(text)
            {
                editTemplate = text;
            }
        });
        
        var dfrd = $.Deferred(),
        //ta   = $('<textarea class="elfinder-file-edit" rows="20" id="'+id+'-ta">'+fm.escape(content)+'</textarea>'),
        save = function() {
//            ta.editor && ta.editor.save(ta[0], ta.editor.instance);
//            dfrd.resolve(ta.getContent());
//            fm.elfinderdialog('close');
            console.log('apretó Save');
        },
        cancel = function() {
            dfrd.reject();
   //         ta.elfinderdialog('close');
            console.log('apretó Cancel');
        },
        opts = {
            title   : file.name,
            width   : self.options.dialogWidth || 450,
            buttons : {},
            close   : function() { 
                $(this).elfinderdialog('destroy'); 
            },
            open    : function() { 
                fm.disable();
            }
					
        };
        opts.buttons[fm.i18n('Save')]   = save;
        opts.buttons[fm.i18n('Cancel')] = cancel
        fm.dialog(editTemplate , opts).attr('id', id);
        return dfrd.promise();
    },
		
    /**
    * Get file content and
    * open dialog with textarea to edit file content
    *
    * @param  String  file hash
    * @return jQuery.Deferred
    **/
    edit = function(file) {
        var hash   = file.hash,
            opts   = fm.options,
            dfrd   = $.Deferred(), 
            data   = {
                cmd : 'file', 
                target : hash
            },
            url    = fm.url(hash) || fm.options.url,
            id    = 'edit-'+fm.namespace+'-'+file.hash,
            d = fm.getUI().find('#'+id), 
            error;
			
			
        if (d.length) {
            d.elfinderdialog('toTop');
            return dfrd.resolve();
        }
			
        if (!file.read || !file.write) {
            error = ['errOpen', file.name, 'errPerm']
            fm.error(error)
            return dfrd.reject(error);
        }
			
        fm.request({
            data   : {
                cmd : 'get', 
                target  : hash
            },
            notify : {
                type : 'openfile', 
                cnt : 1
            },
            syncOnFail : true
        })
        .done(function(data) {
            dialog(id, file, data.content)
                .done(function(content) {
                            fm.request({
                                options : {type : 'post'},
                                data : {
                                    cmd     : 'put',
                                    target  : hash,
                                    content : content
                                },
                                notify : {type : 'save', cnt : 1},
                                syncOnFail : true
                            })
                            .fail(function(error) {
                                dfrd.reject(error);
                            })
                            .done(function(data) {
                                    data.changed && data.changed.length && fm.change(data);
                                    dfrd.resolve(data);
                            });
                    })
                })
                .fail(function(error) {
                    dfrd.reject(error);
                })
                return dfrd.promise();
    };
	
    this.shortcuts = [{
        pattern     : 'ctrl+e'
    }];
	
    this.init = function() {
        this.onlyMimes = this.options.mimes || []
    }
	
    this.getstate = function(sel) {
        var sel = this.files(sel),
        cnt = sel.length;

        return !this._disabled && cnt ? 0 : -1;
    }
	
    this.exec = function(hashes) {
        var files = this.files(hashes),
        list  = [],
        file;

        if (this.disabled()) {
            return $.Deferred().reject();
        }

        while ((file = files.shift())) {
            list.push(edit(file));
        }
		
        return list.length 
        ? $.when.apply(null, list)
        : $.Deferred().reject();
    }

}